package com.springboot.juc;

import java.util.concurrent.TimeUnit;

/**
 * @Author wangzs
 * @Version 1.0
 * @Description 常青树信息
 * @Date 2021/1/12 23:41
 */
public class TestVolatile {

    public static void main(String[] args) throws InterruptedException {
        ThreadDeam td = new ThreadDeam();
        Thread t = new Thread(td);
        t.start();

        while (true){
            //System.out.println(td.flag);
            if (td.flag){
                System.out.println("--------");
                break;
            }
        }

    }


}
class ThreadDeam implements Runnable{

//    不使用volatile会死锁
//    boolean  flag =false;
    volatile boolean  flag =false;

    @Override
    public void run() {
        try {
            Thread.sleep(200);
        }catch (InterruptedException e){

        }

         flag = true;
        System.out.println("flag是"+flag);
    }
}